package oct2013;

/**
 * @author Yinzi Chen
 * @date Nov 14, 2013
 */
public class MultiplyStrings {
    public String multiply(String num1, String num2) {
        int n = num1.length(), m = num2.length();
        int[] res = new int[n + m];
        for (int i = 0; i < n; ++i) {
            int v1 = num1.charAt(n - i - 1) - '0';
            for (int j = 0; j < m; ++j) {
                int v2 = num2.charAt(m - j - 1) - '0';
                res[i + j] += v1 * v2;
            }
        }
        int carry = 0;
        for (int i = 0; i < n + m; ++i) {
            res[i] += carry;
            carry = res[i] / 10;
            res[i] %= 10;
        }
        StringBuilder sb = new StringBuilder();
        int i = n + m - 1;
        while (i > 0 && res[i] == 0)
            i--;
        while (i >= 0) {
            sb.append(res[i--]);
        }
        return sb.toString();
    }
}
